<template>
  <el-dialog
      title="新增网点"
      width="1000px"
      @close="cancel"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="Form" label-width="120px" hide-required-asterisk>
      <row-col>
        <el-form-item label="网点账号" prop="username">
          <el-input v-model.trim="form.username"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="网点密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
      </row-col>
      <el-form-item label="网点级别" prop="level">
        <el-select v-model="form.level" placeholder="请选择网点级别">
          <el-option v-if="this.$level === 1" label="总部" :value="2"></el-option>
          <el-option v-if="this.$level === 1 || this.$level === 2" label="一级网点" :value="3"></el-option>
          <el-option v-if="this.$level === 1 || this.$level === 3" label="二级网点" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <row-col>
        <!--左-->
        <el-form-item label="网点名称(中)" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="联系方式(中)" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="网点地址(中)" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="经理名称(中)">
          <el-input v-model="form.manager"></el-input>
        </el-form-item>
        <el-form-item label="押金:" prop="deposit">
          <el-input-number v-model="form.deposit" controls-position="right" :min="0"/>
        </el-form-item>
        <!--右-->
        <el-form-item slot="r" label="网点名称(老挝)" prop="laosName">
          <el-input v-model="form.laosName"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="联系方式(老挝)" prop="laosPhone">
          <el-input v-model="form.laosPhone"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="网点地址(老挝)" prop="laosAddress">
          <el-input v-model="form.laosAddress"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="经理名称(老挝)">
          <el-input v-model="form.laosManager"></el-input>
        </el-form-item>
        <el-form-item slot="r" label="加盟费:" prop="initialFree">
          <el-input-number v-model="form.initialFree" controls-position="right" :min="0"/>
        </el-form-item>
      </row-col>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <submit-button ref="Submit" @submit='submit'/>
    </div>
  </el-dialog>
</template>

<script>
  import {addUserApi} from '@/api/site/siteList'
  import {resetForm} from "@/utils/common";
  import {encrypt} from "@/utils/encrypt";

  export default {
    name: "Add",
    data() {
      return {
        visible: false,
        form: {
          username: '', // 用户名
          password: '12345', // 密码
          level: null, // 级别
          name: '', // 名称
          phone: '', // 联系方式
          address: '', // 地址
          manager: '', // 经理
          laosName: '',
          laosPhone: '',
          laosAddress: '',
          laosManager: '',
          deposit: 0, // 押金
          initialFree: 0 // 加盟费
        },
        rules: {
          username: {required: true, message: '请输入账号', trigger: 'blur'},
          password: {required: true, message: '请输入密码', trigger: 'blur'},
          level: {required: true, message: '请选择级别', trigger: 'change'},
          name: {required: true, message: '请输入名称', trigger: 'blur'},
          phone: {required: true, message: '请输入联系方式', trigger: 'blur'},
          address: {required: true, message: '请输入地址', trigger: 'blur'},
          laosName: {required: true, message: '请输入名称', trigger: 'blur'},
          laosPhone: {required: true, message: '请输入联系方式', trigger: 'blur'},
          laosAddress: {required: true, message: '请输入地址', trigger: 'blur'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          data.password = encrypt(data.password);
          this.$refs.Submit.start();
          addUserApi(data).then(result => {
            this.$refs.Submit.stop();
            if (result.status !== 200) return
            this.$emit('update');
            this.cancel()
          }).catch(() => {
            this.$refs.Submit.stop();
          });
        });
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
